using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/**游戏控制类 */
public class GameController : MonoBehaviour
{
    /**关卡集合 */
    public GameObject[] levels;
    
    /**是否加载中 */
    private bool _isLoad = false;
    
    /**游戏状态 */
    private GameState _state = GameState.Start;
    
    /**当前关卡 */
    private int _curLevel = 0;
    
    /**当前总分数 */
    private int _curScore = 0;

    public static GameController instance;

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        _state = GameState.Start;
    }

    private void Update()
    {
        if (_state == GameState.Start && GameMain.instance)
        {
            GameStart();
        }
    }

    /**游戏开始 */
    public void GameStart()
    {
        GotoLevel(0);
        _state = GameState.Progress;
        
        _curScore = 0;
        GameMain.instance.UpdateTotalScore(_curScore);

        // 清空数据
        WeaponMgr.instance.ClearData();
    }
    
    /**游戏胜利 */
    public void GameWin()
    {
        GameMain.instance.ShowGameWinPanel();
        _state = GameState.GameWin;
    }
    
    /**游戏结束 */
    public void GameOver()
    {
        GameMain.instance.ShowGameOverPanel();
        _state = GameState.GameOver;
    }

    /**跳转对应关卡 */
    private void GotoLevel(int levelNum = 0)
    {
        _curLevel = levelNum < levels.Length ? levelNum : 0;
        GameMain.instance.UpdateLevel(GetLevelPrefab());
        
        _isLoad = true;
        Invoke(nameof(LoadSuc), 1.0f);
    }

    /**加载完成 */
    private void LoadSuc()
    {
        _isLoad = false;
    }
    
    /**跳转下一关 */
    public void NextLevel()
    {
        if (_isLoad)
        {
            return;
        }
        GotoLevel(_curLevel + 1);
    }

    /**获取当前关卡预制体 */
    public GameObject GetLevelPrefab()
    {
        return levels[_curLevel];
    }
    
    /**增加分数 */
    public void AddScore(int score)
    {
        _curScore += score;
        GameMain.instance.UpdateTotalScore(_curScore);
    }

}
